package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.entity.DeviceInfo;
import com.markerhub.entity.InspectionTask;
import com.markerhub.entity.RepairRequest;
import com.markerhub.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据统计Controller
 */
@Api(tags = "数据统计管理")
@RestController
@RequestMapping("/api/data-statistics")
public class DataStatisticsController extends BaseController {

    @Autowired
    private DeviceInfoService deviceInfoService;
    
    @Autowired
    private InspectionTaskService inspectionTaskService;
    
    @Autowired
    private RepairRequestService repairRequestService;
    
    @Autowired
    private InspectionRecordService inspectionRecordService;

    /**
     * 获取数据概览
     */
    @ApiOperation(value = "获取数据概览", notes = "获取系统整体数据统计概览")
    @PreAuthorize("hasAuthority('data:statistics:overview')")
    @GetMapping("/overview")
    public Result overview() {
        Map<String, Object> overviewData = new HashMap<>();
        
        // 获取设备总数
        long totalDevices = deviceInfoService.count();
        overviewData.put("totalDevices", totalDevices);
        
        // 获取任务总数
        long totalTasks = inspectionTaskService.count();
        overviewData.put("totalTasks", totalTasks);
        
        // 获取维修请求总数
        long totalRepairs = repairRequestService.count();
        overviewData.put("totalRepairs", totalRepairs);
        
        // 获取巡检记录总数
        long totalRecords = inspectionRecordService.count();
        overviewData.put("totalRecords", totalRecords);
        
        return Result.succ(200, "获取数据概览成功", overviewData);
    }

    /**
     * 获取设备统计
     */
    @ApiOperation(value = "获取设备统计", notes = "获取设备相关统计数据")
    @PreAuthorize("hasAuthority('data:statistics:device')")
    @GetMapping("/device")
    public Result device() {
        Map<String, Object> deviceData = new HashMap<>();
        
        // 获取在线设备数量
        long onlineDevices = deviceInfoService.lambdaQuery().eq(DeviceInfo::getStatus, 1).count();
        deviceData.put("onlineDevices", onlineDevices);
        
        // 获取离线设备数量
        long offlineDevices = deviceInfoService.lambdaQuery().eq(DeviceInfo::getStatus, 2).count();
        deviceData.put("offlineDevices", offlineDevices);
        
        // 获取异常设备数量
        long abnormalDevices = deviceInfoService.lambdaQuery().eq(DeviceInfo::getStatus, 3).count();
        deviceData.put("abnormalDevices", abnormalDevices);
        
        return Result.succ(200, "获取设备统计成功", deviceData);
    }

    /**
     * 获取任务统计
     */
    @ApiOperation(value = "获取任务统计", notes = "获取巡检任务相关统计数据")
    @PreAuthorize("hasAuthority('data:statistics:task')")
    @GetMapping("/task")
    public Result task() {
        Map<String, Object> taskData = new HashMap<>();
        
        // 获取待执行任务数量
        long pendingTasks = inspectionTaskService.lambdaQuery().eq(InspectionTask::getStatus, 1).count();
        taskData.put("pendingTasks", pendingTasks);
        
        // 获取已分配任务数量
        long assignedTasks = inspectionTaskService.lambdaQuery().eq(InspectionTask::getStatus, 2).count();
        taskData.put("assignedTasks", assignedTasks);
        
        // 获取已完成任务数量
        long completedTasks = inspectionTaskService.lambdaQuery().eq(InspectionTask::getStatus, 3).count();
        taskData.put("completedTasks", completedTasks);
        
        // 获取逾期任务数量
        long overdueTasks = inspectionTaskService.lambdaQuery().eq(InspectionTask::getStatus, 4).count();
        taskData.put("overdueTasks", overdueTasks);
        
        return Result.succ(200, "获取任务统计成功", taskData);
    }

    /**
     * 获取维修统计
     */
    @ApiOperation(value = "获取维修统计", notes = "获取维修请求相关统计数据")
    @PreAuthorize("hasAuthority('data:statistics:repair')")
    @GetMapping("/repair")
    public Result repair() {
        Map<String, Object> repairData = new HashMap<>();
        
        // 获取待处理维修请求数量
        long pendingRepairs = repairRequestService.lambdaQuery().eq(RepairRequest::getStatus, 1).count();
        repairData.put("pendingRepairs", pendingRepairs);
        
        // 获取处理中维修请求数量
        long processingRepairs = repairRequestService.lambdaQuery().eq(RepairRequest::getStatus, 2).count();
        repairData.put("processingRepairs", processingRepairs);
        
        // 获取已完成维修请求数量
        long completedRepairs = repairRequestService.lambdaQuery().eq(RepairRequest::getStatus, 3).count();
        repairData.put("completedRepairs", completedRepairs);
        
        return Result.succ(200, "获取维修统计成功", repairData);
    }

    /**
     * 获取趋势统计
     */
    @ApiOperation(value = "获取趋势统计", notes = "获取系统运行趋势统计数据")
    @PreAuthorize("hasAuthority('data:statistics:trend')")
    @GetMapping("/trend")
    public Result trend() {
        // 这里可以根据需要实现更复杂的趋势统计逻辑
        // 例如按月或按周统计任务完成情况、维修请求数量等
        Map<String, Object> trendData = new HashMap<>();
        
        // 示例：获取最近7天的任务完成趋势
        trendData.put("recentTaskCompletion", new int[]{12, 18, 15, 22, 20, 16, 25});
        
        // 示例：获取最近7天的维修请求趋势
        trendData.put("recentRepairRequests", new int[]{5, 8, 6, 10, 9, 7, 12});
        
        // 示例：获取最近7天的设备异常趋势
        trendData.put("recentDeviceAbnormalities", new int[]{2, 3, 1, 4, 2, 1, 3});
        
        return Result.succ(200, "获取趋势统计成功", trendData);
    }
}